package com.zenithsun.pic.picsubject.controller;

import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.syzg.base.login.model.LoginObject;
import com.syzg.common.controller.BaseController;
import com.syzg.common.model.WebInfo;
import com.syzg.common.util.MethodUtil;
import com.syzg.common.util.SearchMap;
import com.syzg.common.util.SystemUtil;
import com.syzg.common.model.WebObj;
import com.syzg.common.util.PKCreator;
import com.syzg.common.exception.BaseException;
import com.syzg.core.page.PageResult;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zenithsun.common.bean.OutMessage;
import com.zenithsun.common.bean.OutMessageState;
import com.zenithsun.pic.picdetail.model.Picdetail;
import com.zenithsun.pic.picdetail.service.PicdetailService;
import com.zenithsun.pic.picreview.service.PicReviewService;
import com.zenithsun.pic.picsubject.model.Picsubject;
import com.zenithsun.pic.picsubject.service.PicsubjectService;

@Controller
@RequestMapping("/path/picsubject")
public class PicsubjectController extends BaseController{
	
	static Logger log = LoggerFactory.getLogger(PicsubjectController.class);
	
	@Resource
	private PicsubjectService picsubjectService;
	@Resource
	private PicReviewService picReviewService;
	@Resource
	private PicdetailService picdetailService;
	
	
	@RequestMapping("/listPicsubject")
	public ModelAndView listPicsubject(WebObj webObj)throws Exception{
		//CSRFTokenManager.validateForm(request);
		ModelAndView mv = new ModelAndView("/zenithsun/pic/picsubject/listPicsubject.jsp");
        return mv;
	}
	
	
	@RequestMapping("/shareListPicsubject")
	public ModelAndView shareListPicsubject(WebObj webObj)throws Exception{
		ModelAndView mv = new ModelAndView("/zenithsun/pic/picsubject/shareListPicsubject.jsp");
        return mv;
	}
	
	/**
	 * 我的图片库分页
	 * @param pic 
	 * @param pageNo 当前页
	 * @param limit 页行数
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/shareListPicsubjectJson", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String newsShareJson(Picsubject pic,int pageNo,int limit,
			HttpServletResponse response,HttpServletRequest request) throws Exception {
		//获得日期条件，日期查询
		String year = request.getParameter("year");
		String month = request.getParameter("month");
		OutMessage outMessage = new OutMessage();
		pic.setAllowShare(1);
		PageResult result = picsubjectService.findPicSubjectByList(pageNo, limit, pic,year,month);
		outMessage.setContent(result);
		outMessage.setState(OutMessageState.Success);
		return JSON.toJSONString(outMessage);
	}
	
	/**
	 * 我的图片库分页
	 * @param pic 
	 * @param pageNo 当前页
	 * @param limit 页行数
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/listPicsubjectJson", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String newsCarouselJson(Picsubject pic,int pageNo,int limit,
			HttpServletResponse response,HttpServletRequest request)
			throws Exception {
		OutMessage outMessage = new OutMessage();
		//获得日期条件，日期查询
		String year = request.getParameter("year");
		String month = request.getParameter("month");
		//设置当前用户id
		LoginObject loginObj = SystemUtil.getLoginObject();
		pic.setCreateUserId(loginObj.getPersonId());
		PageResult result = picsubjectService.findPicSubjectByList(pageNo, limit, pic,year,month);
		outMessage.setContent(result);
		outMessage.setState(OutMessageState.Success);
		return JSON.toJSONString(outMessage);
	}
	
	@RequestMapping("/initPicsubject")
	public ModelAndView initPicsubject() throws Exception {
		ModelAndView mv = new ModelAndView("/zenithsun/pic/picsubject/addPicsubject.jsp");
		try {
			mv.addObject("picsubject", new Picsubject());
			/*LoginObject loginObj = SystemUtil.getLoginObject();
			this.picsubject.setCreatePersonId(new String(loginObj.getPersonId()));
			this.picsubject.setCreator(loginObj.getPersonName());
			this.picsubject.setCreateTime(new Date());*/
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/addPicsubject")
	public @ResponseBody String addPicsubject(Picsubject picsubject) throws Exception {
		WebObj webObj = new WebObj();
		try {
			LoginObject loginObj = SystemUtil.getLoginObject();
			picsubject.setCreateUserId(loginObj.getPersonId());
			picsubject.setCreator(loginObj.getPersonName());
			picsubject.setCreateTime(new Date());
			picsubject.setId(PKCreator.getPk());
			picsubjectService.create(picsubject);
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("操作失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/viewPicsubject")
	public ModelAndView viewPicsubject(Picsubject picsubject,ModelAndView mv) throws Exception {
		mv.setViewName("/zenithsun/pic/picsubject/viewPicsubject.jsp");
		try {
			String id = picsubject.getId();
			Picsubject picsubjectVO = picsubjectService.findObjectById(id);
			mv.addObject("picsubject",picsubjectVO);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	@RequestMapping("/editPicsubject")
	public ModelAndView editPicsubject(String id,HttpServletRequest request) throws Exception {
		//CSRFTokenManager.validateForm(request);
		ModelAndView mv = new ModelAndView("/zenithsun/pic/picsubject/editPicsubject.jsp");
		try {
			Picsubject picsubject = picsubjectService.findObjectById(id);
			mv.addObject("picsubject", picsubject);
			WebObj webObj = new WebObj();
			mv.addObject("webObj",webObj);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return mv;
	}
	
	/**
	 * 编辑图片主题
	 * @param id ID
	 * @param toCheck 是否提交审核
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modPicsubject")
	public @ResponseBody String modPicsubject(Picsubject vo,boolean toCheck) throws Exception {
		WebObj webObj = new WebObj();
		try {
			Picsubject picsubject = picsubjectService.findObjectById(vo.getId());
			picsubject = (Picsubject) MethodUtil.copyProperties(picsubject, vo);
			picsubjectService.update(picsubject);
			//保存意见
			picReviewService.saveReview(picsubject, toCheck);
			
			setControllerStatus(webObj,WebInfo.STATUS_SUCCESS);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
			setControllerStatus(webObj,WebInfo.STATUS_FAILURE);
			webObj.getWebInfo().getErrorList().add("操作失败");
		}
		return new JSONObject(webObj).toString();
	}
	
	@RequestMapping("/delPicsubject")
	public ModelAndView delPicsubject(String id,WebObj webObj) throws Exception {
		String[] ids = webObj.getSelectedRow();
		try {
			/*if(ids == null) { //删除单个
				if(id == null) {
					ids = new String[0];
				} else {
					ids = new String[]{String.valueOf(id)};
				}
			}*/
			ids = new String[]{String.valueOf(id)};
			String[] idsStr = new String[ids.length];
			int i = 0;
			for(String idVO: ids) {
				idsStr[i++] = new String(idVO);
			}
			SearchMap searchMap = SearchMap.forClass(Picdetail.class);
			searchMap.eq("picsubject.id", id);
			picdetailService.getCount(searchMap);
			
			picsubjectService.delete(idsStr);
		} catch (Exception e) {
			log.error(BaseException.addExceptionLog(e));
		}
		return listPicsubject(webObj);
	}
}
